package com.huohuzhihui.wxmp.controller;

import com.huohuzhihui.common.annotation.Log;
import com.huohuzhihui.common.core.controller.BaseController;
import com.huohuzhihui.common.core.domain.AjaxResult;
import com.huohuzhihui.common.core.domain.TreeSelect;
import com.huohuzhihui.common.core.domain.entity.SysDept;
import com.huohuzhihui.common.core.page.TableDataInfo;
import com.huohuzhihui.common.enums.BusinessType;
import com.huohuzhihui.common.utils.poi.ExcelUtil;
import com.huohuzhihui.wxmp.entity.WxAccount;
import com.huohuzhihui.wxmp.service.WxAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.*;

/**
 * 公众号账号Controller
 * 
 * @author huohuzhihui
 * @date 2021-01-05
 */
@RestController
@RequestMapping("/wxmp/account")
public class WxAccountController extends BaseController
{
    @Autowired
    private WxAccountService wxAccountService;

    /**
     * 查询公众号账号列表
     */
    @PreAuthorize("@ss.hasPermi('wxmp:account:list')")
    @GetMapping("/list")
    public TableDataInfo list(WxAccount wxAccount)
    {
        startPage();
        List<WxAccount> list = wxAccountService.selectWxAccountList(wxAccount);
        return getDataTable(list);
    }

    /**
     * 导出公众号账号列表
     */
    @PreAuthorize("@ss.hasPermi('wxmp:account:export')")
    @Log(title = "公众号账号", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WxAccount wxAccount)
    {
        List<WxAccount> list = wxAccountService.selectWxAccountList(wxAccount);
        ExcelUtil<WxAccount> util = new ExcelUtil<WxAccount>(WxAccount.class);
        return util.exportExcel(list, "account");
    }

    /**
     * 获取公众号账号详细信息
     */
    @PreAuthorize("@ss.hasPermi('wxmp:account:query')")
    @GetMapping(value = "/{appid}")
    public AjaxResult getInfo(@PathVariable("appid") String appid)
    {
        return AjaxResult.success(wxAccountService.getById(appid));
    }

    /**
     * 新增公众号账号
     */
    @PreAuthorize("@ss.hasPermi('wxmp:account:add')")
    @Log(title = "公众号账号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxAccount wxAccount)
    {
        try {
            wxAccountService.save(wxAccount);
        }catch (Exception e){
            return AjaxResult.error("添加失败"+e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 修改公众号账号
     */
    @PreAuthorize("@ss.hasPermi('wxmp:account:edit')")
    @Log(title = "公众号账号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxAccount wxAccount)
    {
        try {
            wxAccountService.save(wxAccount);
        }catch (Exception e){
            return AjaxResult.error("修改失败"+e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 删除公众号账号
     */
    @PreAuthorize("@ss.hasPermi('wxmp:account:remove')")
    @Log(title = "公众号账号", businessType = BusinessType.DELETE)
	@DeleteMapping("/{appids}")
    public AjaxResult remove(@PathVariable String[] appids)
    {
        return AjaxResult.success(wxAccountService.removeByIds(Arrays.asList(appids)));
    }

    /**
     * 获取部门下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(WxAccount account)
    {
        List<WxAccount> list = wxAccountService.selectWxAccountList(account);
        List<Map<String,Object>> res = new ArrayList<>();
        for (WxAccount wxAccount : list) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",account.getAppid());
            map.put("label",account.getName());
            map.put("children",null);
            res.add(map);
        }
        return AjaxResult.success(res   );
    }


}
